/*
 * Copyright (c) 2011 Samuel Ventura All Rights Reserved
 */
package dev2net;

import gnu.io.SerialPort;
import java.io.IOException;

/**
 *
 * @author samuel
 */
class ComConfig extends Thread {

    private String portname;
    private int tcpport;
    private int baudrate;
    private int databits;
    private int parity;
    private int stopbits;

    public ComConfig(String config) throws IOException {
        String[] parts = config.split(",");

        this.portname = parsePortName(parts[0]);
        this.tcpport = parseTcpPort(parts[1]);
        this.baudrate = parseBaudRate(parts[2]);
        this.databits = parseDataBits(parts[3]);
        this.parity = parseParity(parts[4]);
        this.stopbits = parseStopBits(parts[5]);
    }

    public int getBaudrate() {
        return baudrate;
    }

    public int getDatabits() {
        return databits;
    }

    public int getParity() {
        return parity;
    }

    public String getPortname() {
        return portname;
    }

    public int getStopbits() {
        return stopbits;
    }

    public int getTcpport() {
        return tcpport;
    }

    private int parseTcpPort(String string) {
        try {
            int v = Integer.parseInt(string);
            if (v<1 || v>65535) throw new RuntimeException("Invalid tcp port range");
            return v;
        } catch (NumberFormatException ex) {
            throw new RuntimeException("Invalid tcp port format");
        }
    }

    private int parseBaudRate(String string) {
        if ("2400".equals(string)) return 2400;
        if ("4800".equals(string)) return 4800;
        if ("9600".equals(string)) return 9600;
        if ("19200".equals(string)) return 19200;
        if ("38400".equals(string)) return 38400;
        if ("57600".equals(string)) return 57600;
        if ("115200".equals(string)) return 115200;
        throw new RuntimeException("Invalid baudrate format");
    }

    private int parseDataBits(String string) {
        if ("5".equals(string)) return SerialPort.DATABITS_5;
        if ("6".equals(string)) return SerialPort.DATABITS_6;
        if ("7".equals(string)) return SerialPort.DATABITS_7;
        if ("8".equals(string)) return SerialPort.DATABITS_8;
        throw new RuntimeException("Invalid databits format");
    }

    private int parseParity(String string) {
        if ("e".equals(string)) return SerialPort.PARITY_EVEN;
        if ("m".equals(string)) return SerialPort.PARITY_MARK;
        if ("n".equals(string)) return SerialPort.PARITY_NONE;
        if ("o".equals(string)) return SerialPort.PARITY_ODD;
        if ("s".equals(string)) return SerialPort.PARITY_SPACE;
        throw new RuntimeException("Invalid parity format");
    }

    private int parseStopBits(String string) {
        if ("1".equals(string)) return SerialPort.STOPBITS_1;
        if ("1.5".equals(string)) return SerialPort.STOPBITS_1_5;
        if ("2".equals(string)) return SerialPort.STOPBITS_2;
        throw new RuntimeException("Invalid stopbits format");
    }

    private String parsePortName(String string) {
        return string;
    }

}
